package com.example.boke.service.impl;

import com.example.boke.entity.Comment;
import com.example.boke.mapper.CommentMapper;
import com.example.boke.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-05-12
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    public List<Comment> getallcomment(List<Comment> comments){

        for (Comment comment : comments)
        {
            List<Comment> fatherChildren = new ArrayList<>();

            findChildren(comment, fatherChildren);

            comment.setCommentList(fatherChildren);
        }

        return comments;
    }

    private void findChildren(Comment comment, List<Comment> fatherChildren) {

        //直接找的子级评论
        List<Comment> comments = comment.getCommentList();

        for (Comment comment1 :comments)
        {
            if (!comment1.getCommentList().isEmpty())
            {
                findChildren(comment1,fatherChildren);
            }

            fatherChildren.add(comment1);

            comment.setCommentList(new ArrayList<>());
        }

    }

}
